﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
namespace AESUtil
{
    public class SM4Util
    {
        private static byte[] ProcessData(IBufferedCipher cipher, byte[] data)
        {
            byte[] output = new byte[cipher.GetOutputSize(data.Length)];
            int len = cipher.ProcessBytes(data, 0, data.Length, output, 0);
            cipher.DoFinal(output, len);
            return output;
        }
        public static String Encrypt(String plainText)
        {
            byte[] keyBytes = Convert.FromBase64String(Properties.Settings.Default.KEY); // 转换为字节数组
            byte[] ivBytes = Convert.FromBase64String(Properties.Settings.Default.IV);

            var engine = new SM4Engine();
            var cipher = new PaddedBufferedBlockCipher(
                new CbcBlockCipher(engine), new Pkcs7Padding());
            cipher.Init(true, new ParametersWithIV(new KeyParameter(keyBytes), ivBytes));
            return Convert.ToBase64String(ProcessData(cipher, Encoding.UTF8.GetBytes(plainText))); 
        }
        public static String Decrypt(String str)
        {
            byte[] keyBytes = Convert.FromBase64String(Properties.Settings.Default.KEY); // 转换为字节数组
            byte[] ivBytes = Convert.FromBase64String(Properties.Settings.Default.IV);
                            KeyParameter key = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
                IBufferedCipher c = CipherUtilities.GetCipher("SM4/CBC/PKCS7Padding");
                c.Init(false, new ParametersWithIV(key, ivBytes));
            return Encoding.UTF8.GetString(c.DoFinal(Convert.FromBase64String(str)));

        }
        public static String EncryptUseDefaultKEYIV(String plainText)
        {
            byte[] keyBytes = Convert.FromBase64String(Setting.KEY_SM4); // 转换为字节数组
            byte[] ivBytes = Convert.FromBase64String(Setting.IV_SM4);

            var engine = new SM4Engine();
            var cipher = new PaddedBufferedBlockCipher(
                new CbcBlockCipher(engine), new Pkcs7Padding());
            cipher.Init(true, new ParametersWithIV(new KeyParameter(keyBytes), ivBytes));
            return Convert.ToBase64String(ProcessData(cipher, Encoding.UTF8.GetBytes(plainText)));
        }
        public static String DecryptUseDefaultKEYIV(String str)
        {
            byte[] keyBytes = Convert.FromBase64String(Setting.KEY_SM4); // 转换为字节数组
            byte[] ivBytes = Convert.FromBase64String(Setting.IV_SM4);
            KeyParameter key = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
            IBufferedCipher c = CipherUtilities.GetCipher("SM4/CBC/PKCS7Padding");
            c.Init(false, new ParametersWithIV(key, ivBytes));
            return Encoding.UTF8.GetString(c.DoFinal(Convert.FromBase64String(str)));

        }
    }
}
